package com.yxhl.zoume.core.user.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.ui.fragment.mywallet.HomelistFragment;
import com.yxhl.zoume.core.user.ui.fragment.mywallet.WalletHomeFragment;
import com.yxhl.zoume.core.user.ui.fragment.mywallet.WalletRechargeFailureFragment;
import com.yxhl.zoume.core.user.ui.fragment.mywallet.WalletRechargeFragment;
import com.yxhl.zoume.core.user.ui.fragment.mywallet.WalletRechargeSuccessFragment;
import com.yxhl.zoume.core.user.ui.info.MyWalletEntry;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/3 .
 */
public class MyWalletActivity extends BaseActivityWithToolBar implements BaseView {


    @BindView(R.id.iv_inex_down_arrow)
    ImageView mIvInexDownArrow;
    @BindView(R.id.ll_inex_toolbar_center)
    LinearLayout mLlInexToolbarCenter;

    private String orderSerial;
    private int rechargeMoney;

    public static Intent getCallingIntent(Activity activity) {
        Intent intent = new Intent(activity, MyWalletActivity.class);
        return intent;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFrameContainer();
        mLlInexToolbarCenter.setClickable(false);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 初始化显示内容
     */
    private void initFrameContainer() {
        addFragment(R.id.fl_my_wallet_container, WalletHomeFragment.newInstance());
    }

    /**
     * 显示相应的页面
     *
     * @param walletEntry
     */
    public void showRelevantFragment(MyWalletEntry walletEntry) {
        Fragment fragment = fragmentProvider(walletEntry);
        replaceContentFragment(R.id.fl_my_wallet_container, fragment, fragment.getClass().getSimpleName(), true);
    }


    /**
     * 显示支付的结果页面
     *
     * @param walletEntry
     * @param money
     */
    public void showRechargeResultFragment(MyWalletEntry walletEntry, int money) {
        rechargeMoney = money;
        showRelevantFragment(walletEntry);
    }


    /**
     * fragment tag
     *
     * @param containerId
     * @param fragment
     * @param tag
     * @param addToBackStack
     */
    private void replaceContentFragment(int containerId, Fragment fragment, String tag, Boolean addToBackStack) {
        if (containerId != 0 && fragment != null) {
            FragmentManager fragmentManager = this.getSupportFragmentManager();
            if (fragmentManager != null) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                if (fragmentTransaction != null) {
                    if (addToBackStack) {
                        fragmentTransaction.addToBackStack(null);
                    }
                    fragmentTransaction.replace(containerId, fragment, tag);
                    fragmentTransaction.commitAllowingStateLoss();
                }
            }
        }
    }


    private Fragment fragmentProvider(MyWalletEntry walletEntry) {
        Fragment fragment;
        switch (walletEntry) {
            case INCOME_EXPENSES:
                fragment = HomelistFragment.newInstance();
                break;
            case RECHARGE:
                fragment = WalletRechargeFragment.newInstance();
                break;
            case RECHARGE_SUCCESS: // 充值成功
                fragment = WalletRechargeSuccessFragment.newInstance(rechargeMoney);
                break;
            case RECHARGE_FAILURE: // 充值失败
                fragment = WalletRechargeFailureFragment.newInstance(rechargeMoney);
                break;
            default:
                fragment = WalletHomeFragment.newInstance();
                break;
        }
        isShowTriangle(walletEntry);
        return fragment;
    }

    private void isShowTriangle(MyWalletEntry walletEntry) {
        switch (walletEntry) {
            case INCOME_EXPENSES:
                showTriangle();
                break;

            default:
                break;
        }
    }

    public void hideTriangleAndClickable() {
        mIvInexDownArrow.setVisibility(View.GONE);
        mLlInexToolbarCenter.setClickable(false);
    }

    public void showTriangle() {
        mIvInexDownArrow.setVisibility(View.VISIBLE);
        mLlInexToolbarCenter.setClickable(true);
    }


    @OnClick(R.id.ll_inex_toolbar_center)
    public void onShowPopupWindowClick() {
        HomelistFragment fragment = (HomelistFragment)
                getSupportFragmentManager().findFragmentByTag(HomelistFragment.class.getSimpleName());
        fragment.showPopupWindow();
        triangleAnimationStart();

    }

    public void triangleAnimationStart() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(200);
        rotateAnimation.setFillAfter(true);
        mIvInexDownArrow.startAnimation(rotateAnimation);
    }

    public void triangleAnimationEnd() {
        RotateAnimation rotateAnimation = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(200);
        rotateAnimation.setFillAfter(true);
        mIvInexDownArrow.startAnimation(rotateAnimation);
    }


    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
